using CMS.Application.Common.Interface;
using CMS.Domain.Entity;
using Microsoft.AspNetCore.Mvc;

namespace CMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class DeleteBlogController : ControllerBase
    {
        private readonly IRepository<Blog> _blog;

        public DeleteBlogController(IRepository<Blog> blog)
        {
            _blog = blog;
        }

        [HttpDelete("/blog/{id}")]
        public async Task<object> Blog(Guid id)
        {
            //根据Id查询数据库中相对应的文章
            var BlogId = _blog.Table.FirstOrDefault(x => x.BlogId == id);

            //如果id不为空就是查询到id，那么删除该条数据，反之删除失败
            if (BlogId != null)
            {
                await _blog.DeleteAsync(BlogId,true);
                return new
                    {

                        code = "200",

                        data = "",

                        msg = "删除成功"

                    };
            }else
            {
                return new
                    {

                        code = "400",

                        data = "",

                        msg = "删除失败"

                    };
            }

        }
    }
}